package com.right.health.app.ui.registered.fragment;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.magic.basic.widget.refresh.OnLastItemVisibleListener;
import com.magic.basic.widget.refresh.SwipeListRefreshLayout;
import com.magics.http.listener.OnHttpListener;
import com.magics.http.model.Request;
import com.magics.http.model.Response;
import com.right.health.app.R;
import com.right.health.app.base.BasePresenterFragment;
import com.right.health.app.bean.PayClinicBean;
import com.right.health.app.bean.PayClinicListResponse;
import com.right.health.app.net.OrderApi;
import com.right.health.app.ui.registered.PayClinicDetailActivity;
import com.right.health.app.ui.registered.PayClinicOrderActivity;
import com.right.health.app.ui.registered.adapter.PayClinicAdapter;
import com.right.health.app.widget.ProgressLayout;
import com.vendor.social.utils.CollectionUtil;

import java.util.ArrayList;
import java.util.List;

public class PayClinicFragment extends BasePresenterFragment implements OnHttpListener, OnLastItemVisibleListener, SwipeRefreshLayout.OnRefreshListener {

    private static final String BUNDLE_TYPE_KEY = "type";
    public static final String BUNDELE_ID_KEY = "patientId";

    private static final int REFRESH_LIST_DODE = 670;
    private static final int LOAD_MORE_DODE = 671;
    private ListView mListView;

    private PayClinicAdapter mAdapter;

    private SwipeListRefreshLayout mSwipeListRefreshLayout;


    private List<PayClinicBean> mList = new ArrayList<>();

    private OrderApi mOrderApi;
    private String mType;
    private String mPatientId;
    private int mPage;


    public static PayClinicFragment newInstance(String type,String patientId) {
        PayClinicFragment fragment = new PayClinicFragment();
        Bundle bundle = new Bundle();
        bundle.putString(BUNDLE_TYPE_KEY, type);
        bundle.putString(BUNDELE_ID_KEY, patientId);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle bundle, String s) {
        return layoutInflater.inflate(R.layout.fragment_pay_clicnic, viewGroup, false);
    }

    @Override
    public void findView() {
        mListView = (ListView) findViewById(R.id.listView);
        mListView.setEmptyView(findViewById(R.id.empty_view));
        mSwipeListRefreshLayout = (SwipeListRefreshLayout) findViewById(R.id.refresh_view);
        mSwipeListRefreshLayout.setOnRefreshListener(this);
        mAdapter = new PayClinicAdapter(getActivity());
        mListView.setAdapter(mAdapter);
        mListView.setOnItemClickListener((parent, view, position, id) -> {
            if (TextUtils.equals(mType, "paid")) {
                PayClinicDetailActivity.launch(getActivity(), mAdapter.getItem(position).getPrescriptionid());
            } else {
                PayClinicOrderActivity.launch(getActivity(), mAdapter.getItem(position).getPrescriptionid());

            }
        });
    }

    @Override
    public void initialize() {
        if (getArguments() != null) {
            mType = getArguments().getString(BUNDLE_TYPE_KEY);
            mPatientId = getArguments().getString(BUNDELE_ID_KEY);
        }
        mOrderApi = new OrderApi();
        mOrderApi.setListener(this);
        mSwipeListRefreshLayout.setRefreshing(true);
        mOrderApi.setTag(mPage);
        mOrderApi.addRequestCode(REFRESH_LIST_DODE);
        mOrderApi.getPayClinicList(mType, mPatientId, mPage);
    }

    public void setPatientId(String patientId) {
        mPatientId = patientId;
        mSwipeListRefreshLayout.setRefreshing(true);
        onRefresh();
    }

    @Override
    public void onRefresh() {
        mPage = 1;
        mList.clear();
        mOrderApi.setTag(mPage);
        mOrderApi.addRequestCode(REFRESH_LIST_DODE);
        mOrderApi.getPayClinicList(mType, mPatientId, mPage);
    }

    @Override
    public void onLastItemVisible() {
        mOrderApi.setTag(mPage);
        mOrderApi.addRequestCode(LOAD_MORE_DODE);
        mOrderApi.getPayClinicList(mType, mPatientId, mPage + 1);
    }

    @Override
    public void onResponse(Request request, Response response) {
        if (!response.isResponseFail()) {
            List<PayClinicBean> list;
            PayClinicListResponse payClinicListResponse = null;
            try {
                payClinicListResponse = response.cast(PayClinicListResponse.class);

                if (TextUtils.equals(mType, "unPay")) {
                    list = payClinicListResponse.unpaidinfos;
                } else {
                    list = payClinicListResponse.paidinfos;
                }

                if ((int) request.tag == mPage) {
                    mPage++;
                } else {
                    return;
                }
                if (request.requestCode == REFRESH_LIST_DODE) {
                    mSwipeListRefreshLayout.setRefreshing(false);
                    mSwipeListRefreshLayout.onRefreshComplete();
                    mList.clear();
                }
                if (payClinicListResponse.page != null) {
                    mSwipeListRefreshLayout.setCanLoadMore(!payClinicListResponse.page.lastPage);
                }
                if (!CollectionUtil.isEmpty(list)) {
                    mList.addAll(list);
                }
            } catch (Exception e) {
                Log.i("TAG", "onResponse: " + e.getMessage());
            }
        }
        mAdapter.setDataSource(mList);
    }


}
